package guijavacommander.actions;

import guijavacommander.FilePanel;
import guijavacommander.JavaCommander;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * User: Deady
* Date: 17.07.2009
* Time: 12:31:06
*/
public class RenameFileAction extends AbstractAction {
    private final FilePanel l;

    public RenameFileAction(FilePanel l) {
        this.l = l;
    }

    public void actionPerformed(ActionEvent e) {
        File file = l.getCurrentFile();
        //todo edit with http://www.jroller.com/santhosh/date/20050607
        String name = JOptionPane.showInputDialog(JavaCommander.instance, "Rename", file.getName());
        if (name!=null) {
            final File dest = new File(file.getParent(), name);
            boolean res = file.renameTo(dest);
            if (!res) {
                JOptionPane.showMessageDialog(JavaCommander.instance, "Can't rename file "+file.getName());
            }
            else {
                try {
                    l.selectFile(dest);
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                l.refresh();
            }
        }
    }
}
